
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getxdemo/Controller.dart';

void main() {
  ///1
  runApp(GetMaterialApp(
    initialRoute: "/",
    defaultTransition: Transition.leftToRight,
    getPages: [
      GetPage(name: "/home", page:()=>Home(),transition:Transition.leftToRight),
      GetPage(name: "/other", page: ()=>Other()),
      GetPage(name: "/login/", page: ()=>Login())
    ],
    /// 路由的中间件
    routingCallback:(routes){
      print("------${routes.current}");
    } ,
    home:Home(),
  ));
}

class Home extends StatelessWidget {
  /// 依赖注入
  final Controller  controller = Get.put(Controller());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('getx demo'),
        centerTitle: true,
      ),
      body: GestureDetector(
        onTap: (){
          // Get.to(Other());
          Get.to(
              Other(),
              transition:
              Transition.leftToRight,
              fullscreenDialog:true,
              // id:
              popGesture: true,
              duration: Duration(milliseconds: 1000),
              arguments: {'name':'zhangsan','age':23}
             );
          // Get.off(Other());
          // Get.offAll(Other());
        },
        child: Column(
          children: [
            Column(
              children: [
                GetBuilder<Controller>(
                    init: Controller(),
                    builder: (controller){
                  return Text("${controller.count1}");
                }),
                ElevatedButton(onPressed: (){
                  controller.getBuilderincrement1();

                }, child: Text('getbuilder')),
                /// 局部刷新
                GetX<Controller>(
                  builder: (controller){
                    return Text(controller.count1.value.toString());
                  },
                ),
                GetX<Controller>(
                  builder: (controller){
                    return Text(controller.count1.value.toString());
                  },
                ),
                GetX<Controller>(
                  builder: (controller){
                    return Text(controller.count1.value.toString());
                  },
                ),
                Obx(()=>Text(controller.count1.toString())),
                Obx(()=>Text("${controller.user.value.name}")),
                Obx(()=>Text(controller.list.toString())),
                ElevatedButton(onPressed: (){
                   controller.increment1();
                }, child: Text('+')),
                ElevatedButton(onPressed: (){
                  controller.user.update((user) {
                    user.name = "lisi";
                    user.address = "kkkkkk";
                  });
                  controller.deincrece();

                }, child: Text('update user')),
              ],
              // GetBuilder<SubjectController>(
              //   builder: (controller) {
              //     return Container();
              //   },
              // )
              // Obx(() {
              //   return Container();
              // })
            ),
            ElevatedButton(onPressed: (){
              Get.bottomSheet(
                  Container(
                    height: 300,
                    color: Colors.white,
                    child: ListView(
                      children: [
                        ListTile(
                          title: Text("1"),
                          onTap: (){
                               Get.toNamed("/login",parameters: {"address":"nanshanqu"});
                               Get.toNamed("/login");
                          },
                        ),
                        ListTile(
                          title: Text("1"),
                        ),
                        ListTile(
                          title: Text("1"),
                          onTap: (){
                              Get.toNamed("/other",parameters: {"haha":"lllllll"});
                          },
                        ),
                        ListTile(
                          title: Text("1"),
                          onTap: (){
                              Get.offAndToNamed("/login");
                          },
                        ),
                      ],
                    ),
                  ),
                backgroundColor: Colors.blue,
                barrierColor: Colors.black.withOpacity(0.3),
                enableDrag: true,

              );
            }, child: Text("sheet")),
            ElevatedButton(
                onPressed: (){
                  Get.defaultDialog(
                    title: "titile",
                    titleStyle: TextStyle(color: Colors.red),
                    content: Column(
                      children: [
                        Text("message1"),
                        Text("message1"),
                        Text("message1"),
                        Text("message1"),
                        Text("message1"),
                      ],
                    ),
                    textCancel: "cancel",
                    textConfirm: "confirm",
                    cancelTextColor: Colors.red,
                    confirmTextColor: Colors.blue,
                    cancel: ElevatedButton(onPressed: (){
                      Get.back();
                    }, child: Text("canncel")),
                    confirm: ElevatedButton(onPressed: (){

                    }, child: Text("confirm")),
                    onCancel: (){

                    },
                    onConfirm: (){

                    },
                    backgroundColor: Colors.black.withAlpha(5),
                    barrierDismissible: false,
                    buttonColor: Colors.blue,
                    radius: 6.0
                  );
                },
                child: Text("dialog")),
            ElevatedButton(
                onPressed: (){
                  Get.snackbar(
                      '提示',
                      "有新消息送达",
                    colorText: Colors.red,
                    duration: Duration(milliseconds: 3000),
                    snackPosition: SnackPosition.TOP,
                    titleText: Text("title"),
                    messageText: Column(
                      children: [
                        Icon(Icons.add),
                        Icon(Icons.add),
                        Icon(Icons.add),
                        Icon(Icons.add),
                        Icon(Icons.add),
                      ],
                    ),
                    icon: Icon(Icons.print),
                    borderWidth: 1,
                    borderRadius: 6,
                    borderColor: Colors.black,
                    backgroundColor: Colors.blue
                  );
                },
                child: Text("snackbar")),
            Container(
              width: 200,
              height: 200,
              color: Colors.pink,
              child: Align(
                  alignment: Alignment.center,
                  child:Obx(
                        ()=>Text(
                      controller.count.toString(),
                      style: TextStyle(color: Colors.red,fontSize: 26),
                    ),
                  )
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: (){
           controller.increment();
        },
      ),
    );
  }
}

class Other extends StatelessWidget {
  final Controller controller = Get.find();
  // final controller1 = Get.find<Controller>();

  var result = Get.arguments;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("other"),
        leading: ElevatedButton(
          onPressed: (){
            Get.back();
          },
          child: Icon(Icons.arrow_back),
        ),
      ),
      body: Center(
        child: Text("${controller.count.toString()}and ${Get.arguments['name']}"),
      ),
    );
  }
}

class Login extends StatelessWidget {
  const Login({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("${Get.parameters["user"]}")
      ),
      body: Container(
        child: Text("login...."),
      ),
    );
  }
}







